package com.beecloud.microservice.user.service;

import com.beecloud.microservice.user.commom.Converts;
import com.beecloud.microservice.user.commom.EncryptUtils;
import com.beecloud.microservice.user.commom.RichString;
import com.beecloud.microservice.user.dao.UserDao;
import com.beecloud.microservice.user.domain.dto.UserDTO;
import com.beecloud.microservice.user.domain.dto.UserPageDTO;
import com.beecloud.microservice.user.domain.error.ParameterWrongException;
import com.beecloud.microservice.user.domain.error.ResourceNotFoundException;
import com.beecloud.microservice.user.entities.UserEntity;
import com.beecloud.microservice.user.web.parameter.UserRegisterParam;
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import static com.beecloud.microservice.user.commom.PageUtils.*;
import static com.beecloud.microservice.user.commom.RichList.convertList;
import static com.beecloud.microservice.user.commom.RichList.head;

public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;

    @Override
    public UserDTO changePassword(String userId, String oldPassword, String newPassword) {
        UserEntity userEntity = findOneUserByUserId(userId);
        String passwordInDb = userEntity.getPassword();
        String encryptedOldPassword = EncryptUtils.encryptPassword(oldPassword);
        if (!encryptedOldPassword.equals(passwordInDb))
            throw new ParameterWrongException("The old password is incorrect.");
        String encryptNewPassword = EncryptUtils.encryptPassword(newPassword);
        userEntity.setPassword(encryptNewPassword);
        userDao.updateUser(userEntity);
        return Converts.entity2DTO(userEntity);
    }


    @Override
    public UserPageDTO queryUsers(String name, int page, int pageSize) {
        String formatName = RichString.trimOp(name).orElse(null);
        if (invalidPage(page) || invalidPageSize(pageSize))
            throw new ParameterWrongException("Page or pageSize parameter must be positive parameter.");
        int totalNum = userDao.countUserByName(formatName);
        List<UserEntity> userEntities = hasNextPage(page, pageSize, totalNum) ? userDao.queryUsersByName(formatName, page, pageSize) : Lists.newArrayList();
        UserPageDTO userPageDTO = new UserPageDTO(convertList(userEntities, Converts::entity2DTO));
        fullPageMeta(page, pageSize, totalNum, userPageDTO);
        return userPageDTO;
    }

    @Override
    public UserDTO queryUserById(String userId) {
        if (StringUtils.isBlank(userId))
            throw new ParameterWrongException("User id must be non blank.");
        UserEntity entity = findOneUserByUserId(userId);
        return Converts.entity2DTO(entity);
    }

    @Override
    public boolean createNotification(String userId, String content) {
        return false;
    }

    @Override
    public UserDTO createUser(UserRegisterParam param) {
        return null;
    }

    @Override
    public List<UserDTO> queryUsers(String[] userIds) {
        return null;
    }

    private UserEntity findOneUserByUserId(String userId) {
        List<UserEntity> users = userDao.findUsersByIds(new String[]{userId});
        return head(users).orElseThrow(ResourceNotFoundException::new);
    }
}
